<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "asr_keyword_ywlc_classify".
 *
 * @property int $id 业务流程的表
 * @property string $name 名字
 * @property int $audio_id 所属流程的ID
 * @property int $file_id 文件ID
 * @property int $parent_id 父ID
 */
class AsrKeywordYwlcClassify extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'asr_keyword_ywlc_classify';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['id', 'audio_id', 'file_id', 'parent_id'], 'integer'],
            [['name'], 'string', 'max' => 40],
            [['id'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'Ywlc ID',
            'name' => 'Name',
            'audio_id' => 'Audio ID',
            'file_id' => 'File ID',
            'parent_id' => 'Parent ID',
        ];
    }


    /**
     * 获取所有的分类
     */
    public function getCategories()
    {
        $data = self::find()->all();
        $data = ArrayHelper::toArray($data);
        return $data;
    }

    /**
     *遍历出各个子类 获得树状结构的数组
     */
    public static function getTree($data,$pid = 0,$lev = 1)
    {
        $tree = [];
        foreach($data as $value){
            if($value['parent_id'] == $pid){
                $value['name'] = str_repeat('',$lev).$value['name'];
                $value['level'] = $lev;
                $tree[] = $value;
                $tree = array_merge($tree,self::getTree($data,$value['id'],$lev+1));
            }
        }
        return $tree;
    }

    /**
     * 得到相应  id  对应的  分类名  数组
     */
    public function getOptions()
    {
        $data = $this->getCategories();
        $tree = $this->getTree($data);
        $list = ['添加顶级分类'];
        foreach($tree as $value){
            $list[$value['parent_id']] = $value['name'];
        }
        return $list;
    }
}
